﻿using Google.Apis.Customsearch.v1;
using Google.Apis.Customsearch.v1.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace WebBotDetector
{
    class Program
    {
        private static string nonspamFolderPath = "nonspam-train";
        private static string spamFolderPath = "spam-train";

        static void Main(string[] args)
        {
            List<string> spamTestList = InitSpamTests();
            List<string> nonspamTestList = InitNonspamTests();

            foreach (var x in spamTestList)
            {
                Console.WriteLine(WebChecker.GetBingTotalResults(x, false));
                Console.WriteLine(WebChecker.GetBingTotalResults(x, true));
                Console.WriteLine(WebChecker.GetGoogleTotalResults(x, false));
                Console.WriteLine(WebChecker.GetGoogleTotalResults(x, true));
                Console.WriteLine();
            }
            Console.WriteLine("Spams");
            foreach (var x in nonspamTestList)
            {
                Console.WriteLine(WebChecker.GetBingTotalResults(x, false));
                Console.WriteLine(WebChecker.GetBingTotalResults(x, true));
                Console.WriteLine(WebChecker.GetGoogleTotalResults(x, false));
                Console.WriteLine(WebChecker.GetGoogleTotalResults(x, true));
                Console.WriteLine();
            }
            Console.WriteLine();
        }

        private static List<string> InitNonspamTests()
        {
            List<string> nonspamList = new List<string>();
            int counter = 10;
            foreach (string file in Directory.EnumerateFiles(nonspamFolderPath, "*.txt"))
            {
                if (counter == 0)
                    break;
                counter--;
                string text = File.ReadLines(file).First();
                string[] words = text.Split(' ');

                int wordCount = words.Count();
                if (wordCount > 1)
                {
                    string queryText = "";
                    int x = 1;

                    while (x < 5 && x < wordCount)
                    {
                        queryText += words[x];
                        queryText += " ";
                        x++;
                    }

                    nonspamList.Add(queryText);
                }
                else
                    counter++;
            }
            return nonspamList;
        }

        private static List<string> InitSpamTests()
        {
            List<string> spamList = new List<string>();
            int counter = 10;
            foreach (string file in Directory.EnumerateFiles(spamFolderPath, "*.txt"))
            {
                if (counter == 0)
                    break;
                counter--;
                string readText = File.ReadLines(file).First();
                Console.WriteLine(readText);
                string[] words = readText.Split(' ');

                int wordCount = words.Count();
                if (wordCount > 1)
                {
                    string queryText = "";
                    int x = 1;

                    while (x < 5 && x < wordCount)
                    {
                        queryText += words[x];
                        queryText += " ";
                        x++;
                    }

                    spamList.Add(queryText);
                }
                else
                    counter++;
            }
            return spamList;
        }
    }
}